package com.operate.service;

import com.operate.dao.ReservationMapper;
import com.operate.bean.VO.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/11/6 3:27
 */
@Slf4j
@Service
public class ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * @name: reservationMonthList
     * @description: 预约 渠道数量统计（近12个月）
     * @auther: ZeckZheng
     */
    public List<WRMonthNumVO> reservationMonthList() {
        List<WRMonthNumVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            WRMonthNumVO vo = new WRMonthNumVO();
            vo.setType("R");
            vo.setStatTime(statTime);
            vo.setH5Num(0L);
            vo.setIpadNum(0L);
            vo.setKioskNum(0L);
            list.add(vo);
        }
        List<WRMonthNumVO> waitList = reservationMapper.reservationMonthList();
        Map<String, WRMonthNumVO> map = new HashMap<>();
        if (waitList != null && !waitList.isEmpty()) {
            for (WRMonthNumVO waitNumVO : waitList) {
                map.put(waitNumVO.getStatTime(), waitNumVO);
            }
        }
        for (WRMonthNumVO vo : list) {
            if (map.containsKey(vo.getStatTime())) {
                vo.setH5Num(map.get(vo.getStatTime()).getH5Num());
                vo.setKioskNum(map.get(vo.getStatTime()).getKioskNum());
                vo.setIpadNum(map.get(vo.getStatTime()).getIpadNum());
            }
        }
        return list;

    }

    /**
     * @name: reservationTimeStat
     * @description: 近30天 预约时间段统计
     * @auther: ZeckZheng
     */
    public List<CommonVO> reservationTimeStat() {
        List<CommonVO> list = new ArrayList<>();
        int r_today = 0;    //预约当天
        int r_tomorrow = 0; //第二天
        int r_1 = 0;       //前7天
        int r_2 = 0;       //8-14天
        int r_3 = 0;      //15-21天
        int r_4 = 0;      //22-28天
        int r_5 = 0;      //29之后

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<ReservationTimeStatVO> statList = reservationMapper.reservationStatTime();
        for (ReservationTimeStatVO vo : statList) {
            LocalDate ct = LocalDate.parse(vo.getCreateTime(), dtf);
            LocalDate rt = LocalDate.parse(vo.getReservationTime(), dtf);
            Integer days = Math.toIntExact(rt.toEpochDay() - ct.toEpochDay());
            if (days == 0) {
                r_today += 1;
            } else if (days == 1) {
                r_tomorrow += 1;
            } else if (days <= 7) {
                r_1 += 1;
            } else if (days <= 14) {
                r_2 += 1;
            } else if (days <= 21) {
                r_3 += 1;
            } else if (days <= 28) {
                r_4 += 1;
            } else {
                r_5 += 1;
            }
        }

        CommonVO v1 = new CommonVO().setType("预约当天").setNum(r_today).setMark("RT");
        CommonVO v2 = new CommonVO().setType("预约第二天").setNum(r_tomorrow).setMark("RT");
        CommonVO v3 = new CommonVO().setType("预约前7天").setNum(r_1 + r_tomorrow).setMark("RT");  //包含t_tomorrow
        CommonVO v4 = new CommonVO().setType("预约8-14天").setNum(r_2).setMark("RT");
        CommonVO v5 = new CommonVO().setType("预约15-21天").setNum(r_3).setMark("RT");
        CommonVO v6 = new CommonVO().setType("预约22-28天").setNum(r_4).setMark("RT");
        CommonVO v7 = new CommonVO().setType("预约29天之后").setNum(r_5).setMark("RT");

        list.add(v1);
        list.add(v2);
        list.add(v3);
        list.add(v4);
        list.add(v5);
        list.add(v6);
        list.add(v7);

        return list;
    }

    /**
     * @name: dinersNumStatList
     * @description: 就餐人数规模统计
     * @auther: ZeckZheng
     */
    public List<DinersNumStatVO> dinersNumStatList() {
        List<DinersNumStatVO> list = new ArrayList<>();
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
        for (int i = 11; i >= 0; i--) {
            String statTime = localDate.minusMonths(i).format(dtf);
            DinersNumStatVO vo = new DinersNumStatVO();
            vo.setType("R");
            vo.setStatTime(statTime);
            vo.setLevel1(0L);
            vo.setLevel2(0L);
            vo.setLevel3(0L);
            list.add(vo);
        }
        List<DinersNumStatVO> dinersList = reservationMapper.dinersNumStatList();
        Map<String, DinersNumStatVO> map = new HashMap<>();
        if (dinersList != null && !dinersList.isEmpty()) {
            for (DinersNumStatVO vo : dinersList) {
                map.put(vo.getStatTime(), vo);
            }
        }
        for (DinersNumStatVO vo : list) {
            if (map.containsKey(vo.getStatTime())) {
                vo.setLevel1(map.get(vo.getStatTime()).getLevel1());
                vo.setLevel2(map.get(vo.getStatTime()).getLevel2());
                vo.setLevel3(map.get(vo.getStatTime()).getLevel3());
            }
        }
        return list;
    }

    public List<ShareNumVO> reservationRegularCustomerStat() {
        List<ShareNumVO> list = new ArrayList<>();
//        LocalDate localDate = LocalDate.now();
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM");
//        for (int i = 11; i >= 0; i--) {
//            String statTime = localDate.minusMonths(i).format(dtf);
//            ShareNumVO vo = new ShareNumVO();
//            vo.setStatTime(statTime);
//            vo.setNum1(0L);
//            vo.setNum2(0L);
//            vo.setMark("R");
//            list.add(vo);
//        }
//        Map<String, Map<String, Object>> regularMap = reservationMapper.reservationRegularCustomer();
//        Map<String, Map<String, Object>> totalMap = reservationMapper.reservationTotalCustomerMonth();
//        for (ShareNumVO vo : list) {
//            if (regularMap.containsKey(vo.getStatTime())) {
//                vo.setNum1((Long) regularMap.get(vo.getStatTime()).get("regularCustomer"));
//            }
//            if (totalMap.containsKey(vo.getStatTime())) {
//                vo.setNum2((Long) totalMap.get(vo.getStatTime()).get("totalCustomer"));
//            }
//        }
        return list;
    }


}
